package com.hyd.fragment;
import java.util.ArrayList;

import com.hyd.adapter.TalkItemAdapter;
import com.hyd.project.R;
import com.hyd.project.TalkDetailActvity;
import com.hyd.project.WriteActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class TalkFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
	private SwipeRefreshLayout refreshLayout;
	private ListView myListView;
	private TalkItemAdapter adapter;
	private ArrayList<String> dataList = new ArrayList<String>();
	private ImageView write;
	
	private Handler hander = new Handler()
	{
		public void handleMessage(android.os.Message msg)
		{
	            if(msg.what == 1){
				refreshLayout.setRefreshing(false);
	            }
		};
	};
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.talk_fragment, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		InitView();
		InitData();
		InitClicK();
	}
	private void InitView(){
		refreshLayout = (SwipeRefreshLayout)getActivity().findViewById(R.id.talk_refreshLayout);
		myListView = (ListView)getActivity().findViewById(R.id.talk_lv);
		write = (ImageView)getActivity().findViewById(R.id.write);
		refreshLayout.setOnRefreshListener(this);
		refreshLayout.setColorSchemeResources(android.R.color.holo_green_dark, android.R.color.holo_green_light,
				android.R.color.holo_orange_light, android.R.color.holo_red_light);
	}

	private void InitData(){
		for (int i = 0; i < 8; i++) {
			dataList.add(i + "");
		}
		adapter = new TalkItemAdapter(getActivity(), dataList);
		myListView.setAdapter(adapter);
	}
	
	private void InitClicK(){
		myListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(getActivity(),TalkDetailActvity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
			}
		});
		
		write.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),WriteActivity.class);
				startActivity(intent);
				getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
			}
		});
	}
	@Override
	public void onRefresh() {
		refreshLayout.setRefreshing(true);
		hander.sendEmptyMessageDelayed(1, 2000);  
	}
}
